package cn.codergege.demo.mock

import spock.lang.Specification

/**
 * Created by codergege on 17-3-1.
 */
class PublisherSpec extends Specification {
    Publisher publisher = new Publisher()
    // Mocking interfaces
    Subscriber subscriber = Mock()
    Subscriber subscriber2 = Mock()
    // Mocking class
    NewSubcriber newSubcriber = Mock()

    def setup() {
        publisher.subscribers << subscriber
        publisher.subscribers << subscriber2
        publisher.newSubcriber = newSubcriber
    }

    def "test send"() {
        when:
        publisher.send('hello')

        then:
        1 * subscriber.recieve('hello')
        1 * subscriber2.recieve('hello')
        1 * newSubcriber.recieve('hello')


        def message = 'spock'

        when:
        publisher.send(message)

        then:
        interaction {
            1 * subscriber.recieve(message)
            1 * subscriber2.recieve(message)
            1 * newSubcriber.recieve(message)
        }

    }
}
